import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/index.vue'

Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/logout',
    component: () => import('@/views/logout/index.vue')
  },
  {
    path: '/confirm',
    name: '确认登录',
    component: () => import('@/views/login/confirm.vue')
  },
  {
    path: '/wxLogin',
    name: '微信授权登录',
    component: () => import('@/views/login/wxLogin.vue')
  },
  {
    path: '/index',
    name: 'Index',
    component: Index,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index/index.vue')
      },
      {
        path: '/history',
        component: () => import('@/views/history/index.vue')
      },
      {
        path: '/humanity',
        component: () => import('@/views/humanity/index.vue')
      },
      {
        path: '/books',
        component: () => import('@/views/books/index.vue')
      },
      {
        path: '/press',
        component: () => import('@/views/press/index.vue')
      },
      {
        path: '/Audiovisual',
        component: () => import('@/views/Audiovisual/index.vue')
      },
      {
        path: '/activity',
        component: () => import('@/views/activity/index.vue')
      },
      {
        path: '/indexs',
        component: () => import('@/views/indexs/index.vue')
      }



    ]
  },
  {
    path: '/BookDetails',
    component: () => import('@/views/BookDetails/index.vue')
  },
  {
    path: '/pucha',
    name:'pucha',
    component: () => import('@/views/pucha/index.vue')
  },
  {
    path: '/SearchFor',
    component: () => import('@/views/SearchFor/index.vue'),
    redirect: '/s1',
    children: [
      {
        path: '/s1',
        component: () => import('@/views/SearchFor/s1.vue')
      },
      {
        path: '/s2',
        component: () => import('@/views/SearchFor/s2.vue')
      },
      {
        path: '/s3',
        component: () => import('@/views/SearchFor/s3.vue')
      },
      {
        path: '/s4',
        component: () => import('@/views/SearchFor/s4.vue')
      },
      {
        path: '/s5',
        component: () => import('@/views/SearchFor/s5.vue')
      },
      {
        path: '/s6',
        component: () => import('@/views/SearchFor/s6.vue')
      },
      {
        path: '/s7',
        component: () => import('@/views/SearchFor/s7.vue')
      },
    ]
  },
  {
    path: '/retrieval',
    component: () => import('@/views/retrieval/index.vue')
  },
  {
    path: '/audio',
    name: '音频详情',
    component: () => import('@/views/audio/index.vue')
  },
  {
    path: '/video',
    name: '音频详情',
    component: () => import('@/views/video/index.vue')
  },
  {
    path: '/images',
    name: '音频详情',
    component: () => import('@/views/tupian/index.vue')
  },
  {
    path: '/picture',
    name: '图片库',
    component: () => import('@/views/picture/index.vue')
  },
  {
    path: '/literature',
    name: '文献详情',
    component: () => import('@/views/literature/index.vue')
  },
  {
    path: '/ccr',
    name: '传承人',
    component: () => import('@/views/literature/ccr.vue')
  },
  {
    path: '/huiyi',
    name: '会议',
    component: () => import('@/views/literature/huiyi.vue')
  },
  {
    path: '/jidi',
    name: '基地',
    component: () => import('@/views/literature/jidis.vue')
  },
  {
    path: '/hd',
    name: '活动',
    component: () => import('@/views/literature/huodong.vue')
  },
  {
    path: '/txxm',
    name: '调查项目档案',
    component: () => import('@/views/literature/txxm.vue')
  },
  {
    path: '/swiper',
    name: '文献详情',
    component: () => import('@/views/literature/swiper.vue')
  },{
    path: '/lw',
    name: '论文详情',
    component: () => import('@/views/lilun/lw.vue')
  },{
    path: '/pcxs',
    name: '普查线索档案',
    component: () => import('@/views/literature/pcxs.vue')
  },{
    path: '/kt',
    name: '普查线索档案',
    component: () => import('@/views/lilun/kt.vue')
  },{
    path: '/jb',
    name: '简报',
    component: () => import('@/views/BookDetails/jb.vue')
  },{
    path: '/qk',
    name: '期刊',
    component: () => import('@/views/BookDetails/qk.vue')
  },{
    path: '/sc',
    name: ' 我的收藏',
    component: () => import('@/views/user/index.vue')
  },
]

const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) { // 解决vue页面之间跳转，页面不是在顶部的问题
    return { x: 0, y: 0 }
}
})
router.beforeEach((to, from, next) => {
  if(to.path === '/logout') return next()
  if(to.path === '/confirm') return next()
  let isLogin = window.sessionStorage.getItem('loginInfo');
  if (isLogin) {
    //如果用户信息存在则往下执行。
    next()
  } else {
    //如果用户token不存在则跳转到login页面
    if (to.path === '/') {
      next()
    } else {
      next('/')
    }
  }
  next()
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
